
// button object
local PANEL = {};


/*------------------------------------
	initialize the visualization
	settings
------------------------------------*/
function PANEL:Init( )

	// set the dialog title
	self:SetTitle( "#musex_vissettings_title" );
	
	// set the width
	self:SetWide( 245 );

	// get the visualization name
	local vis = MuseX.GetSetting( "visualization" );
	
	// get visualization table
	local tbl = MuseX.VisualizationList[ vis ];
	
	// list of items for holding values
	self.Items = {};
	
	// starting Y position
	local ypos = 30;
	
	// loop through list of settings
	for k, setting in pairs( tbl.Settings ) do
	
		// check if its a number setting
		if ( setting[ 3 ] == MXSETTING_NUM ) then
		
			// create the number slider
			local item = vgui.Create( "MXNumSlider", self );
			
			// set the position
			item:SetPos( 5, ypos );
			
			// set the width
			item:SetWide( self:GetWide() - 10 );
			
			// set the text
			item:SetText( setting[ 1 ] );
			
			// set the value
			item:SetValue( tbl:GetSetting( setting[ 1 ] ) );
			
			item.VisSettingData = {
			
				[ 'setting' ] = setting,
				[ 'SetValue' ] = "SetValue",
				[ 'GetValue' ] = "GetValue"
			
			};
			self.Items[ #self.Items + 1 ] = item;
			
			// check if they supplied formatting information
			if ( setting[ 4 ] && type( setting[ 4 ] ) == "table" ) then
			
				// check for Min information
				if ( setting[ 4 ][ 'Min' ] ) then
				
					// set the minimum number
					item:SetMin( setting[ 4 ][ 'Min' ] );
				
				end
				
				// check for Max information
				if ( setting[ 4 ][ 'Max' ] ) then
				
					// set the max number
					item:SetMax( setting[ 4 ][ 'Max' ] );
				
				end
				
				// check for Dec information
				if ( setting[ 4 ][ 'Dec' ] ) then
				
					// set the decimals
					item:SetDecimals( setting[ 4 ][ 'Dec' ] );
				
				end
			
			end
			
			// move the Y position
			ypos = ypos + 50;
			
		// check if it is a boolean setting
		elseif ( setting[ 3 ] == MXSETTING_BOOL ) then
		
			// create the checkbox
			local item = vgui.Create( "MXCheckBoxLabel", self );
			
			// set the position
			item:SetPos( 5, ypos );
			
			// set the width
			item:SetWide( self:GetWide() - 10 );
			
			// set the text
			item:SetText( setting[ 1 ] );
			
			// set the starting value
			item:SetValue( tbl:GetSetting( setting[ 1 ] ) );
			
			item.VisSettingData = {
			
				[ 'setting' ] = setting,
				[ 'SetValue' ] = "SetValue",
				[ 'GetValue' ] = "GetChecked"
			
			};
			self.Items[ #self.Items + 1 ] = item;
			
			// move the Y position
			ypos = ypos + item:GetTall() + 5;
			
		// check if it is a string setting
		elseif ( setting[ 3 ] == MXSETTING_STRING ) then
		
			// create the setting label
			local label = vgui.Create( "MXLabel", self );
			
			// set the position
			label:SetPos( 8, ypos );
			
			// set the label text
			label:SetText( setting[ 1 ] );
			
			// size to contents
			label:SizeToContents();
			
			// move the Y position
			ypos = ypos + label:GetTall() + 3;
			
			// create the text entry
			local item = vgui.Create( "MXTextEntry", self );
			
			// set the position
			item:SetPos( 5, ypos );
			
			// set the text
			item:SetText( tbl:GetSetting( setting[ 1 ] ) );
			
			item.VisSettingData = {
			
				[ 'setting' ] = setting,
				[ 'SetValue' ] = "SetText",
				[ 'GetValue' ] = "GetValue"
			
			};
			self.Items[ #self.Items + 1 ] = item;
			
			// set the size
			item:SetSize( self:GetWide() - 10, 20 );
			
			// move the Y position
			ypos = ypos + 30;
		
		end
	
	end
	
	// move the Y position
	ypos = ypos + 10;
	
	// create the cancel button
	self.Cancel = vgui.Create( "MXButton", self );
	// set the text
	self.Cancel:SetText( "#musex_cancel" );
	// setup the cancel function
	self.Cancel.DoClick = function( self )
	
		// close
		self.GetParent( self ):Close();
		
	end
	
	// size & position cancel button
	self.Cancel:SetSize( 75, 20 );
	self.Cancel:SetPos( 5, ypos );
	
	
	// create the default button
	self.Default = vgui.Create( "MXButton", self );
	// set the text
	self.Default:SetText( "#musex_default" );
	// setup the default function
	self.Default.DoClick = function( self )
	
		// get the parent
		local par = self:GetParent();
	
		// list through the Items
		for _, item in pairs( par.Items ) do
		
			// grab the function name
			local func = item.VisSettingData[ 'SetValue' ];
			
			// call the function to set the value
			item[ func ]( item, item.VisSettingData[ 'setting' ][ 2 ] );
		
		end
		
	end
	
	// size & position default button
	self.Default:SetSize( 75, 20 );
	self.Default:SetPos( 85, ypos );
	
	
	// create the okay button
	self.Okay = vgui.Create( "MXButton", self );
	// set the text
	self.Okay:SetText( "#musex_okay" );
	// setup the okay function
	self.Okay.DoClick = function( self )
	
		// get the visualization name
		local vis = MuseX.GetSetting( "visualization" );
		
		// get visualization table
		local tbl = MuseX.VisualizationList[ vis ];
	
		// get the parent
		local par = self:GetParent();
	
		// list through the Items
		for _, item in pairs( par.Items ) do
		
			// grab the function name
			local func = item.VisSettingData[ 'GetValue' ];
			
			// get the key
			local key = item.VisSettingData[ 'setting' ][ 1 ];
			
			// call the function to get the value
			local value = item[ func ]( item );
			
			// save it
			tbl:SetSetting( key, value );
		
		end
		
		// close
		self.GetParent( self ):Close();
		
	end
	
	// size & position okay button
	self.Okay:SetSize( 75, 20 );
	self.Okay:SetPos( self:GetWide() - 80, ypos );
	
	// set the height of window
	self:SetTall( ypos + 30 );
	
	// custom derma hooks
	Derma_Hook( self, "Paint", "Paint", "MXMainFrame" );
	Derma_Hook( self, "ApplySchemeSettings", "Scheme", "MXFrame" );
	self.AnimIn = Derma_Anim( "OpenAnim", self, self.OpenAnim );
	self.AnimOut = Derma_Anim( "CloseAnim", self, self.CloseAnim );
	
end


/*------------------------------------
	open the manager
------------------------------------*/
function PANEL:Open( )

	// ignore if we're already visible
	if ( self:IsVisible() ) then
	
		// bye
		return;
		
	end
	
	// close menus
	CloseDermaMenus();
	
	// take focus and be visible
	self:MakePopup();
	self:SetVisible( true );
	self:DoModal();
	
	// enable keyboard input
	self:SetKeyboardInputEnabled( true );
	
	// enable mouse input
	self:SetMouseInputEnabled( true );
	
	// halt the close animation
	self.AnimOut:Stop();
	
	// start the open animation
	self.AnimIn:Start( 0.1 );

end


/*------------------------------------
	handle the opening animation
------------------------------------*/
function PANEL:OpenAnim( anim, delta, data )

	// check if it has been started
	if ( anim.Started ) then
	
		// reset alpha
		self:SetAlpha( 0 );
		
	end
	
	// check if it has been finished
	if ( anim.Finished ) then
	
		// fully opaque
		self:SetAlpha( 255 );
		
		// all done
		return;
		
	end
	
	// animate the alpha
	self:SetAlpha( 255 * delta );

end


/*------------------------------------
	close the manager
------------------------------------*/
function PANEL:Close( skip )
	
	// close menus
	CloseDermaMenus();
	
	// halt the opening animation
	self.AnimIn:Stop();
	
	// check if we're skipping the animation
	if ( skip ) then
	
		// restore alpha
		self:SetAlpha( 255 );
		
		// not visible
		self:SetVisible( false );
	
	else
	
		// start the close animation
		self.AnimOut:Start( 0.1 );
		
	end

end


/*------------------------------------
	handle the closing animation
------------------------------------*/
function PANEL:CloseAnim( anim, delta, data )

	// check if the animation has started
	if ( anim.Started ) then
	
		// restore the alpha
		self:SetAlpha( 255 );
		
	end
	
	// check if the animation has finished
	if ( anim.Finished ) then
	
		// store alpha
		self:SetAlpha( 255 );
		
		// set as not visible
		self:SetVisible( false );
		
		// destroy
		self:Remove();
		
		// all done
		return;
		
	end
	
	// animate the alpha
	self:SetAlpha( 255 - 255 * delta );

end



/*------------------------------------
	manager think
------------------------------------*/
function PANEL:Think( )

	// call BaseClass
	self.BaseClass.Think( self );
	
	// run the animation functions
	self.AnimIn:Run();
	self.AnimOut:Run();

end


/*------------------------------------
	button paint
------------------------------------*/
function PANEL:Paint( )
	
	// all done
	return false;

end

// define
derma.DefineControl( "MXVisualizationSettings", "", PANEL, "DFrame" );
